import { Component, OnInit, Input, ComponentRef } from '@angular/core';
import { FundInfoHistoryService } from './fundInfo-history.service';
import * as _ from 'lodash';
import { environment } from '../../../../../environments/environment';
import { Util } from 'app/common/util';

@Component({
  selector: 'app-fundInfo-history',
  templateUrl: './fundInfo-history.component.html',
  styleUrls: ['./fundInfo-history.component.scss'],
  providers: [FundInfoHistoryService]
})
export class FundInfoHistoryComponent implements OnInit {
  // 原变更注册产品的内部编码
  public nRegisterChgNo: any;
  // 产品文件内部编码
  public nFileFundNo: any;

  // lodash
  public _: any = _;
  //托管行字典值
  // public bankDictionaryList = [];

  // 获取单个产品基础信息post
  public singleProductPost = {
    nFundNo: ""
  }
  // 原变更注册基础信息
  public baseInfoPost = {
    sFundCode: '',
    sFundName: '',
    sFullName: '',
    sFundType: '1',
    sFundCategory: '',
    sIsQdii: '',
    sInvestStyle: '',
    sPmAccount: '',
    sListType: '0',
    sExchange: '',
    sBank: '',
    dBankConfirm: '',
    sInvestScope: '',
    sBenchmark: '',
    sManagementFee: '',
    sTrusteeFee: '',
    sOtherFee: '',
    dIssue: '',
    dEstablishment: '',
    sCreator: '',
    sCreatorNo: '',
    sIsRegisterChg: '',
    nRegisterChgNo: '',
    sStatus: '',
    sStatusRunning: '1',
    sRemark: '',
    dCreateTime: ''
  }
  // 原变更注册审核信息
  public yqmjPostBody = {
    publicissueExamineInfo: {
      sRemarkChg: "",
      sZshRemarkChg: "",
      nFundNo: "",
      sFundCode: "",
      sFundName: "",
      sFullName: "",
      sFundType: "",
      nAuditNo: '',
      dLxCommittee: "",
      dAuditCommittee: "",
      dCsrsSb: "",
      dCsrsBz: "",
      dCsrsSl: "",
      dCsrsFeedback: "",
      dCsrsCommitopt: "",
      dCsrsReply: "",
      dCsrsReplyEnd: "",
      dCsrsAnswer: "",
      sPossessorMt: "",
      dPossessorGg: "",
      dPossessorJy: "",
      dRaisePostponeBh: "",
      dRaisePostponePf: "",
      dRaisePostponeHh: "",
      dRatePossessorMt: "",
      dRatePossessorGg: "",
      dRatePossessorJy: "",
      dChgRecordSb: "",
      dChgSb: "",
      dOtherchgRecordSb: "",
      dOtherChgGg: ""
    }
  }

  // 原变更注册界面判断是否显示产品改造信息
  public bgzcStatu: any;
  public yqmjStatu: any;
  public tzflStatu: any;
  public zsfeStatu: any;
  public qtbgStatu: any;

  // 查询单个审核信息post
  public singleCheckPost = {
    nFundNo: ''
  }
  // 基金经理
  public managerList = [];
  // 原变更注册基金经理多选下拉框
  public oldManagerSelectList = [];
  // 原变更注册创建日期
  public bgzcCreateDate: any;
  // 用于保存显示的附件列表
  public activeFileList = [];
  // 用于保存激活的文件tab大类
  public activeFileType = "";
  // 附件树内码
  // public nFileFundNo: any;
  // 基础文件数据  GM=公募
  public jcwjGMDataList = [];
  // 审批文件数据
  public spwjGMDataList = [];
  // 产品改造文件数据
  public cpgzGMDataList = [];
  // 其他文件数据
  public qtwjGMDataList = [];
  // 历史附件数据
  public historyGMDataList = [];
  // 基础文件
  public jcwjGM: any;
  // 审批文件
  public spwjGM: any;
  // 产品改造文件
  public cpgzwjGM: any;
  // 其他文件
  public qtwjGM: any;
  // 历史附件
  public historyGM: any;
  // 附件历史数据
  public fileHistoryData: any;
  public dictionaryList: any = {
    FUND_CATEGORY_GM: [],
    INVEST_STYLE: [],
    EXCHANGE: []
  }
  constructor(
    public fundInfoHistoryService: FundInfoHistoryService
  ) {

  }

  ngOnInit() {
    // 初始化select2多选下拉框
    window['$'](".select2").select2();
    // 从LocalStorage缓存中取 托管行
    // this.bankDictionaryList = JSON.parse(localStorage.getItem("bankDictionaryList"));

    // 从LocalStorage缓存中取 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));

    // 基金经理
    this.getManagerList();

    // 原变更注册基础信息
    this.getSingleProductInfo(this.nRegisterChgNo);
    // 原变更注册审核信息
    this.getSingleCheckInfo(this.nRegisterChgNo);
    // 原变更注册文件
    this.loadFileTree();

  }

  /**
   * 获取 原变更注册基础信息
   * @param nRegisterChgNo 产品内码
   */
  getSingleProductInfo(nRegisterChgNo) {
    let that = this;
    this.oldManagerSelectList = [];
    this.singleProductPost.nFundNo = nRegisterChgNo;
    this.fundInfoHistoryService.getSingleProductInfo(this.singleProductPost).subscribe(data => {
      if (data) {
        //将获取到的数据赋予baseInfoPost对象
        that.baseInfoPost.sFundName = data.sFundName;
        that.baseInfoPost.sFundCode = data.sFundCode;
        that.baseInfoPost.sFundType = data.sFundType;
        that.baseInfoPost.sFullName = data.sFullName;
        that.baseInfoPost.sFundCategory = data.sFundCategory;
        that.baseInfoPost.sIsQdii = data.sIsQdii;
        that.baseInfoPost.sInvestStyle = data.sInvestStyle;
        that.baseInfoPost.sPmAccount = data.sPmAccount;
        that.baseInfoPost.sListType = data.sListType;
        that.baseInfoPost.sExchange = data.sExchange;
        that.baseInfoPost.sBank = data.sBank;
        that.baseInfoPost.dBankConfirm = data.dBankConfirm;
        that.baseInfoPost.sInvestScope = data.sInvestScope;
        that.baseInfoPost.sBenchmark = data.sBenchmark;
        that.baseInfoPost.sManagementFee = data.sManagementFee;
        that.baseInfoPost.sTrusteeFee = data.sTrusteeFee;
        that.baseInfoPost.sOtherFee = data.sOtherFee;
        that.baseInfoPost.dIssue = data.dIssue;
        that.baseInfoPost.dEstablishment = data.dEstablishment;
        that.baseInfoPost.sCreator = data.sCreator;
        that.baseInfoPost.sCreatorNo = data.sCreatorNo;
        that.baseInfoPost.sIsRegisterChg = data.sIsRegisterChg;
        that.baseInfoPost.nRegisterChgNo = data.nRegisterChgNo;
        that.baseInfoPost.sStatus = data.sStatus;
        that.baseInfoPost.sStatusRunning = data.sStatusRunning;
        that.baseInfoPost.sRemark = data.sRemark;
        that.bgzcCreateDate = data.dCreateTime;
        // 基金经理转换
        if (data.sPmNames) {
          that.oldManagerSelectList = _.concat([], data.sPmNames);
        }
        setTimeout(function () {
          // 初始化select2多选下拉框
          window['$'](".select2").select2();
        }, 1);
      }
    })
  }

  /**
   * 原变更注册审核信息
   * @param nRegisterChgNo 变更注册内码
   */
  getSingleCheckInfo(nRegisterChgNo) {
    this.singleCheckPost.nFundNo = nRegisterChgNo;
    this.bgzcStatu = '';
    this.yqmjStatu = '';
    this.tzflStatu = '';
    this.zsfeStatu = '';
    this.qtbgStatu = '';
    this.yqmjPostBody.publicissueExamineInfo.nFundNo = "";
    this.yqmjPostBody.publicissueExamineInfo.nAuditNo = "";
    this.yqmjPostBody.publicissueExamineInfo.dLxCommittee = "";
    this.yqmjPostBody.publicissueExamineInfo.dAuditCommittee = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsSb = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsBz = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsSl = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsFeedback = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsCommitopt = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsReply = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsReplyEnd = "";
    this.yqmjPostBody.publicissueExamineInfo.dCsrsAnswer = "";
    this.yqmjPostBody.publicissueExamineInfo.sPossessorMt = "";
    this.yqmjPostBody.publicissueExamineInfo.dPossessorGg = "";
    this.yqmjPostBody.publicissueExamineInfo.dPossessorJy = "";
    this.yqmjPostBody.publicissueExamineInfo.dRaisePostponeBh = "";
    this.yqmjPostBody.publicissueExamineInfo.dRaisePostponePf = "";
    this.yqmjPostBody.publicissueExamineInfo.dRaisePostponeHh = "";
    this.yqmjPostBody.publicissueExamineInfo.dRatePossessorMt = "";
    this.yqmjPostBody.publicissueExamineInfo.dRatePossessorGg = "";
    this.yqmjPostBody.publicissueExamineInfo.dRatePossessorJy = "";
    this.yqmjPostBody.publicissueExamineInfo.dChgRecordSb = "";
    this.yqmjPostBody.publicissueExamineInfo.dChgSb = "";
    this.yqmjPostBody.publicissueExamineInfo.dOtherChgGg = "";
    this.yqmjPostBody.publicissueExamineInfo.sRemarkChg = "";
    this.yqmjPostBody.publicissueExamineInfo.sZshRemarkChg = "";
    this.fundInfoHistoryService.getSingleCheckInfo(this.singleCheckPost).subscribe(data => {
      if (data.publicissueExamineInfo !== null) {
        //将获取到的数据赋予baseInfoPost对象
        //查询到的审核信息
        this.yqmjPostBody.publicissueExamineInfo.nFundNo = data.publicissueExamineInfo.nFundNo;
        this.yqmjPostBody.publicissueExamineInfo.nAuditNo = data.publicissueExamineInfo.nAuditNo;
        this.yqmjPostBody.publicissueExamineInfo.dLxCommittee = data.publicissueExamineInfo.dLxCommittee;
        this.yqmjPostBody.publicissueExamineInfo.dAuditCommittee = data.publicissueExamineInfo.dAuditCommittee;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsSb = data.publicissueExamineInfo.dCsrsSb;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsBz = data.publicissueExamineInfo.dCsrsBz;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsSl = data.publicissueExamineInfo.dCsrsSl;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsFeedback = data.publicissueExamineInfo.dCsrsFeedback;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsCommitopt = data.publicissueExamineInfo.dCsrsCommitopt;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsReply = data.publicissueExamineInfo.dCsrsReply;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsReplyEnd = data.publicissueExamineInfo.dCsrsReplyEnd;
        this.yqmjPostBody.publicissueExamineInfo.dCsrsAnswer = data.publicissueExamineInfo.dCsrsAnswer;
        this.yqmjPostBody.publicissueExamineInfo.sPossessorMt = data.publicissueExamineInfo.sPossessorMt;
        this.yqmjPostBody.publicissueExamineInfo.dPossessorGg = data.publicissueExamineInfo.dPossessorGg;
        this.yqmjPostBody.publicissueExamineInfo.dPossessorJy = data.publicissueExamineInfo.dPossessorJy;
        this.yqmjPostBody.publicissueExamineInfo.dRaisePostponeBh = data.publicissueExamineInfo.dRaisePostponeBh;
        this.yqmjPostBody.publicissueExamineInfo.dRaisePostponePf = data.publicissueExamineInfo.dRaisePostponePf;
        this.yqmjPostBody.publicissueExamineInfo.dRaisePostponeHh = data.publicissueExamineInfo.dRaisePostponeHh;
        this.yqmjPostBody.publicissueExamineInfo.dRatePossessorMt = data.publicissueExamineInfo.dRatePossessorMt;
        this.yqmjPostBody.publicissueExamineInfo.dRatePossessorGg = data.publicissueExamineInfo.dRatePossessorGg;
        this.yqmjPostBody.publicissueExamineInfo.dRatePossessorJy = data.publicissueExamineInfo.dRatePossessorJy;
        this.yqmjPostBody.publicissueExamineInfo.dChgRecordSb = data.publicissueExamineInfo.dChgRecordSb;
        this.yqmjPostBody.publicissueExamineInfo.dChgSb = data.publicissueExamineInfo.dChgSb;
        this.yqmjPostBody.publicissueExamineInfo.dOtherChgGg = data.publicissueExamineInfo.dOtherChgGg;
        this.yqmjPostBody.publicissueExamineInfo.dOtherchgRecordSb = data.publicissueExamineInfo.dOtherchgRecordSb;
        this.yqmjPostBody.publicissueExamineInfo.sRemarkChg = data.publicissueExamineInfo.sRemarkChg;
        this.yqmjPostBody.publicissueExamineInfo.sZshRemarkChg = data.publicissueExamineInfo.sZshRemarkChg;
        this.bgzcStatu = data.bgzc;
        this.yqmjStatu = data.yqmj;
        this.tzflStatu = data.tzfl;
        this.zsfeStatu = data.zsfe;
        this.qtbgStatu = data.qtbg;
      }
    })
  }

  // 获取产品树
  loadFileTree() {
    let that = this;
    let getBody = {
      "sFundType": 1,
      "nFundNo": this.nFileFundNo
    }
    this.fundInfoHistoryService.getFileTree(getBody).subscribe((data) => {
      if (data) {
        that.activeFileList = [];
        that.jcwjGMDataList = [];
        that.spwjGMDataList = [];
        that.cpgzGMDataList = [];
        that.qtwjGMDataList = [];
        that.historyGMDataList = [];
        _.forEach(data.subTree, function (item, index) {
          that.activeFileList.push(item.sAttachCode);
          switch (item.sAttachCode) {
            case "jcwjGM":
              that.jcwjGM = item.subTree;
              let _jcwjGMDataList = [];
              _.forEach(that.jcwjGM, function (ite) {          // ite是附件小分类
                _.forEach(ite.fileList, function (it) {         // it是附件
                  it.sFundType = ite.sFundType;                 // 附件类型 2 
                  it["sParentType"] = item.sAttachType;         // 附件大分类名称 产品方案
                  it["sParentCode"] = ite.sParentCode;          // 附件大分类编码 cpfaYDD
                  it["parentType"] = ite.sAttachType;           // 附件小分类名称 产品需求表
                  it["attachCode"] = ite.sAttachCode;           // 附件小分类编码 cpxqbYDD
                  it["subNodeL"] = ite.fileList.length;         // 附件小分类长度
                  it["sType"] = "";                             // 附件小分类名称
                  it["editStatus"] = "0";                       // 附件名编辑状态
                  it["fileName"] = "";                          // 附件修改名称
                  it["fileType"] = "";                          // 附件后缀
                  _jcwjGMDataList.push(it);
                })
                ite.fileList[0]["sType"] = ite.sAttachType;     // 附件小分类名称
              });
              that.jcwjGMDataList = _jcwjGMDataList;
              break;
            case "spwjGM":
              that.spwjGM = item.subTree;
              let _spwjGMDataList = [];
              _.forEach(that.spwjGM, function (ite) {
                _.forEach(ite.fileList, function (it) {
                  it.sFundType = ite.sFundType;
                  it["sParentType"] = item.sAttachType;
                  it["sParentCode"] = ite.sParentCode;
                  it["parentType"] = ite.sAttachType;
                  it["attachCode"] = ite.sAttachCode;
                  it["subNodeL"] = ite.fileList.length;
                  it["sType"] = "";
                  it["editStatus"] = "0";
                  it["fileName"] = "";
                  it["fileType"] = "";
                  _spwjGMDataList.push(it);
                })
                ite.fileList[0]["sType"] = ite.sAttachType;
              });
              that.spwjGMDataList = _spwjGMDataList;
              break;
            case "cpgzwjGM":
              that.cpgzwjGM = item.subTree;
              let _cpgzGMDataList = [];
              _.forEach(that.cpgzwjGM, function (ite) {
                _.forEach(ite.fileList, function (it) {
                  it.sFundType = ite.sFundType;
                  it["sParentType"] = item.sAttachType;
                  it["sParentCode"] = ite.sParentCode;
                  it["parentType"] = ite.sAttachType;
                  it["attachCode"] = ite.sAttachCode;
                  it["subNodeL"] = ite.fileList.length;
                  it["sType"] = "";
                  it["editStatus"] = "0";
                  it["fileName"] = "";
                  it["fileType"] = "";
                  _cpgzGMDataList.push(it);
                })
                ite.fileList[0]["sType"] = ite.sAttachType;
              });
              that.cpgzGMDataList = _cpgzGMDataList;
              break;
            case "qtwjGM":
              that.qtwjGM = item.subTree;
              _.forEach(that.qtwjGM, function (ite) {
                _.forEach(ite.fileList, function (it) {
                  it.sFundType = ite.sFundType;
                  it["sParentType"] = item.sAttachType;
                  it["sParentCode"] = ite.sParentCode;
                  it["parentType"] = ite.sAttachType;
                  it["attachCode"] = ite.sAttachCode;
                  it["subNodeL"] = ite.fileList.length;
                  it["sType"] = "";
                  it["editStatus"] = "0";
                  it["fileName"] = "";
                  it["fileType"] = "";
                  that.qtwjGMDataList.push(it);
                })
                ite.fileList[0]["sType"] = ite.sAttachType;
              });
              break;
            case "historyGM":
              that.historyGM = item.subTree;
              _.forEach(that.historyGM, function (ite) {
                _.forEach(ite.fileList, function (it) {
                  it.sFundType = ite.sFundType;
                  it["sParentType"] = item.sAttachType;
                  it["sParentCode"] = ite.sParentCode;
                  it["parentType"] = ite.sAttachType;
                  it["attachCode"] = ite.sAttachCode;
                  it["subNodeL"] = ite.fileList.length;
                  it["sType"] = "";
                  it["editStatus"] = "0";
                  it["fileName"] = "";
                  it["fileType"] = "";
                  that.historyGMDataList.push(it);
                })
                ite.fileList[0]["sType"] = ite.sAttachType;
              });
              break;
          }
        })
        setTimeout(() => {
          that.activeFileTab();
        }, 200)
      }
    })
  }

  // 下载附件
  downLoadFile(item) {
    if (item.nAttachNo) {
      Util.attachDownload([item.nAttachNo]);
    } else {
      window['swal']("提示", "暂无文件下载", "warning");
    }
  }

  // 查看历史文件
  showHistoryVersion(item) {
    let that = this;
    this.activeFileType = item.sParentCode;
    this.fundInfoHistoryService.getFileHistory(item.nAttachNo).subscribe(data => {
      if (data) {
        that.fileHistoryData = data;
        window['$']("#historyFileModal").modal('show');
      } else {
        window['swal']("提示", "暂无文件", "warning");
      }
    })
  }

  // 关闭 历史文件 modal
  closeHistoryFileModal() {
    window['$']("#historyFileModal").modal('hide');
  }
  // 激活文件tab
  activeFileTab() {
    let that = this;
    window['$']('#tab14').removeClass('active');
    window['$']('#tab_14').removeClass('active');
    window['$']("#jcwjGMContent").attr('aria-expanded', "false");
    window['$']('#tab15').removeClass('active');
    window['$']('#tab_15').removeClass('active');
    window['$']("#spwjGMContent").attr('aria-expanded', "false");
    window['$']('#tab16').removeClass('active');
    window['$']('#tab_16').removeClass('active');
    window['$']("#cpgzwjGMContent").attr('aria-expanded', "false");
    window['$']('#tab17').removeClass('active');
    window['$']('#tab_17').removeClass('active');
    window['$']("#qtwjGMContent").attr('aria-expanded', "false");
    window['$']('#tab18').removeClass('active');
    window['$']('#tab_18').removeClass('active');
    window['$']("#historyGMContent").attr('aria-expanded', "false");
    if (this.activeFileType == "") {
      this.activeFileType = this.activeFileList[0];
    }
    switch (this.activeFileType) {
      case "jcwjGM":
        window['$']('#tab14').addClass('active');
        window['$']('#tab_14').addClass('active');
        window['$']("#jcwjGMContent").attr('aria-expanded', "true");
        break;
      case "spwjGM":
        window['$']('#tab15').addClass('active');
        window['$']('#tab_15').addClass('active');
        window['$']("#spwjGMContent").attr('aria-expanded', "true");
        break;
      case "cpgzwjGM":
        window['$']('#tab16').addClass('active');
        window['$']('#tab_16').addClass('active');
        window['$']("#cpgzwjGMContent").attr('aria-expanded', "true");
        break;
      case "qtwjGM":
        window['$']('#tab17').addClass('active');
        window['$']('#tab_17').addClass('active');
        window['$']("#qtwjGMContent").attr('aria-expanded', "true");
        break;
      case "historyGM":
        window['$']('#tab18').addClass('active');
        window['$']('#tab_18').addClass('active');
        window['$']("#historyGMContent").attr('aria-expanded', "true");
        break;
    }
  }

  // 基金经理
  getManagerList() {
    let that = this;
    this.fundInfoHistoryService.getManagerList().subscribe(data => {
      that.managerList = data;
    })
  }
}
